<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>浏览页面</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css"/>
</head>
<body class="pear-container">

<!-- 顶部查询表单 -->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form top-search-from">

            <div class="layui-form-item">
                <label class="layui-form-label">允许开测试卡</label>
                <div class="layui-input-block">
                    <div name="test_switch" id="test_switch" value=""></div>
                </div>
            </div>

            <!--                    <div class="layui-form-item">-->
            <!--                        <label class="layui-form-label">测试卡2小时状态</label>-->
            <!--                        <div class="layui-input-block">-->
            <!--                            <div name="test2h_switch" id="test2h_switch" value="" ></div>-->
            <!--                        </div>-->
            <!--                    </div>-->
            <!--                    -->
            <!--                    <div class="layui-form-item">-->
            <!--                        <label class="layui-form-label">测试卡6小时状态</label>-->
            <!--                        <div class="layui-input-block">-->
            <!--                            <div name="test6h_switch" id="test6h_switch" value="" ></div>-->
            <!--                        </div>-->
            <!--                    </div>-->
            <!--                    -->
            <!--                    <div class="layui-form-item">-->
            <!--                        <label class="layui-form-label">测试卡12小时状态</label>-->
            <!--                        <div class="layui-input-block">-->
            <!--                            <div name="test12h_switch" id="test12h_switch" value="" ></div>-->
            <!--                        </div>-->
            <!--                    </div>-->

            <div class="layui-form-item">
                <label class="layui-form-label">允许开日卡</label>
                <div class="layui-input-block">
                    <div name="rp_switch" id="rp_switch" value=""></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">允许开周卡</label>
                <div class="layui-input-block">
                    <div name="zp_switch" id="zp_switch" value=""></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">允许开月卡</label>
                <div class="layui-input-block">
                    <div name="zp_switch" id="yp1_switch" value=""></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">允许开季卡</label>
                <div class="layui-input-block">
                    <div name="zp_switch" id="yp3_switch" value=""></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">允许开半年卡</label>
                <div class="layui-input-block">
                    <div name="zp_switch" id="yp6_switch" value=""></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">允许开年卡</label>
                <div class="layui-input-block">
                    <div name="zp_switch" id="np_switch" value=""></div>
                </div>
            </div>

            <div class="layui-form-item layui-inline">
                <label class="layui-form-label"></label>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                    <i class="layui-icon layui-icon-search"></i>查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                    <i class="layui-icon layui-icon-refresh"></i>重置
                </button>
            </div>
            <div class="toggle-btn">
                <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
            </div>
        </form>
    </div>
</div>

<!-- 数据表格 -->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="data-table" lay-filter="data-table"></table>
    </div>
</div>

<!-- 表格顶部工具栏 -->
<!--<script type="text/html" id="table-toolbar">-->
<!--    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.admin.v1lineextend.insert">-->
<!--        <i class="layui-icon layui-icon-add-1"></i>新增-->
<!--    </button>-->
<!--    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove"-->
<!--            permission="app.admin.v1lineextend.delete">-->
<!--        <i class="layui-icon layui-icon-delete"></i>删除-->
<!--    </button>-->
<!--</script>-->

<!-- 表格行工具栏 -->
<script type="text/html" id="table-bar">
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.v1lineextend.update">编辑
    </button>
<!--    <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.v1lineextend.delete">删除-->
<!--    </button>-->
</script>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script src="/app/admin/admin/js/common.js"></script>

<script>

    // 相关常量
    const PRIMARY_KEY = "id";
    const SELECT_API = "/app/admin/v1-line-extend/select";
    const UPDATE_API = "/app/admin/v1-line-extend/update";
    const DELETE_API = "/app/admin/v1-line-extend/delete";
    const INSERT_URL = "/app/admin/v1-line-extend/insert";
    const UPDATE_URL = "/app/admin/v1-line-extend/update";

    // 字段 测试卡状态:0=关闭,1=开启 test_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#test_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#test_switch",
                    name: "test_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // // 字段 测试卡2小时状态:0=关闭,1=开启 test2h_switch
    // layui.use(["jquery", "xmSelect", "popup"], function() {
    //     layui.$.ajax({
    //         url: "/app/admin/dict/getNew/get_open_off_list",
    //         dataType: "json",
    //         success: function (res) {
    //             let value = layui.$("#test2h_switch").attr("value");
    //             let initValue = value ? value.split(",") : [];
    //             layui.xmSelect.render({
    //                 el: "#test2h_switch",
    //                 name: "test2h_switch",
    //                 filterable: true,
    //                 initValue: initValue,
    //                 data: res.data,
    //                 model: {"icon":"hidden","label":{"type":"text"}},
    //                 clickClose: true,
    //                 radio: true,
    //                 layVerify: "",
    //             })
    //             if (res.code) {
    //                 layui.popup.failure(res.msg);
    //             }
    //         }
    //     });
    // });
    //
    // // 字段 测试卡6小时状态:0=关闭,1=开启 test6h_switch
    // layui.use(["jquery", "xmSelect", "popup"], function() {
    //     layui.$.ajax({
    //         url: "/app/admin/dict/getNew/get_open_off_list",
    //         dataType: "json",
    //         success: function (res) {
    //             let value = layui.$("#test6h_switch").attr("value");
    //             let initValue = value ? value.split(",") : [];
    //             layui.xmSelect.render({
    //                 el: "#test6h_switch",
    //                 name: "test6h_switch",
    //                 filterable: true,
    //                 initValue: initValue,
    //                 data: res.data,
    //                 model: {"icon":"hidden","label":{"type":"text"}},
    //                 clickClose: true,
    //                 radio: true,
    //                 layVerify: "",
    //             })
    //             if (res.code) {
    //                 layui.popup.failure(res.msg);
    //             }
    //         }
    //     });
    // });
    //
    // // 字段 测试卡12小时状态:0=关闭,1=开启 test12h_switch
    // layui.use(["jquery", "xmSelect", "popup"], function() {
    //     layui.$.ajax({
    //         url: "/app/admin/dict/getNew/get_open_off_list",
    //         dataType: "json",
    //         success: function (res) {
    //             let value = layui.$("#test12h_switch").attr("value");
    //             let initValue = value ? value.split(",") : [];
    //             layui.xmSelect.render({
    //                 el: "#test12h_switch",
    //                 name: "test12h_switch",
    //                 filterable: true,
    //                 initValue: initValue,
    //                 data: res.data,
    //                 model: {"icon":"hidden","label":{"type":"text"}},
    //                 clickClose: true,
    //                 radio: true,
    //                 layVerify: "",
    //             })
    //             if (res.code) {
    //                 layui.popup.failure(res.msg);
    //             }
    //         }
    //     });
    // });

    // 字段 日卡状态:0=关闭,1=开启 rp_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#rp_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#rp_switch",
                    name: "rp_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // 字段 周卡状态:0=关闭,1=开启 zp_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#zp_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#zp_switch",
                    name: "zp_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // 字段 月卡状态:0=关闭,1=开启 yp1_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#yp1_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#yp1_switch",
                    name: "yp1_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // 字段 季卡状态:0=关闭,1=开启 yp3_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#yp3_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#yp3_switch",
                    name: "yp3_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // 字段 半年卡状态:0=关闭,1=开启 yp6_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#yp6_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#yp6_switch",
                    name: "yp6_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // 字段 年卡状态:0=关闭,1=开启 np_switch
    layui.use(["jquery", "xmSelect", "popup"], function () {
        layui.$.ajax({
            url: "/app/admin/dict/getNew/get_open_off_list",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#np_switch").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#np_switch",
                    name: "np_switch",
                    filterable: true,
                    initValue: initValue,
                    data: res.data,
                    model: {"icon": "hidden", "label": {"type": "text"}},
                    clickClose: true,
                    radio: true,
                    layVerify: "",
                })
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    // 表格渲染
    layui.use(["table", "form", "common", "popup", "util"], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;
        let common = layui.common;
        let util = layui.util;

        // 表头参数
        let cols = [
            {
                type: "checkbox",
                align: "left",
                width: 50,
            }, {
                title: "主键", align: "left",
                field: "id",
                sort: true,
                width: 100,
            }, {
                title: "线路名", align: "left",
                field: "name",
                width: 300,
            },{
                title: "排序",align: "left",
                field: "weight",
                sort: true,
                width: 100,
            },
            // {
            //     title: "权限状态", align: "left",
            //     field: "status",
            //     templet: function (d) {
            //         let field = "status";
            //         form.on("switch(" + field + ")", function (data) {
            //             let load = layer.load();
            //             let postData = {};
            //             postData[field] = data.elem.checked ? 1 : 0;
            //             postData[PRIMARY_KEY] = this.value;
            //             $.post(UPDATE_API, postData, function (res) {
            //                 layer.close(load);
            //                 if (res.code) {
            //                     return layui.popup.failure(res.msg, function () {
            //                         data.elem.checked = !data.elem.checked;
            //                         form.render();
            //                     });
            //                 }
            //                 return layui.popup.success("操作成功");
            //             })
            //         });
            //         let checked = d[field] === 1 ? "checked" : "";
            //         return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
            //     },
            //     width: 100,
            // },
            {
                title: "测试卡数量", align: "left",
                field: "test_number",
                width: 100,
            }, {
                title: "允许开测试卡", align: "left",
                field: "test_switch",
                templet: function (d) {
                    let field = "test_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "允许开2小时", align: "left",
                field: "test2h_switch",
                templet: function (d) {
                    let field = "test2h_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "允许开6小时", align: "left",
                field: "test6h_switch",
                templet: function (d) {
                    let field = "test6h_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "允许开12小时", align: "left",
                field: "test12h_switch",
                templet: function (d) {
                    let field = "test12h_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "最大调换数量", align: "left",
                field: "max_exchange_number",
                width: 120,
            }, {
                title: "日卡价格", align: "left",
                field: "rp",
                width: 100,
            }, {
                title: "周卡价格", align: "left",
                field: "zp",
                width: 100,
            }, {
                title: "月卡价格", align: "left",
                field: "yp1",
                width: 100,
            }, {
                title: "季卡价格", align: "left",
                field: "yp3",
                width: 100,
            }, {
                title: "半年卡价格", align: "left",
                field: "yp6",
                width: 100,
            }, {
                title: "年卡价格", align: "left",
                field: "np",
                width: 100,
            }, {
                title: "允许开日卡", align: "left",
                field: "rp_switch",
                templet: function (d) {
                    let field = "rp_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "允许开周卡", align: "left",
                field: "zp_switch",
                templet: function (d) {
                    let field = "zp_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "允许开月卡", align: "left",
                field: "yp1_switch",
                templet: function (d) {
                    let field = "yp1_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "允许开季卡", align: "left",
                field: "yp3_switch",
                templet: function (d) {
                    let field = "yp3_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "允许开半年卡", align: "left",
                field: "yp6_switch",
                templet: function (d) {
                    let field = "yp6_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "允许开年卡", align: "left",
                field: "np_switch",
                templet: function (d) {
                    let field = "np_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "日卡显示", align: "left",
                field: "rp_show",
                templet: function (d) {
                    let field = "rp_show";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('显示|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            }, {
                title: "周卡显示", align: "left",
                field: "zp_show",
                templet: function (d) {
                    let field = "zp_show";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('显示|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "月卡显示", align: "left",
                field: "yp1_show",
                templet: function (d) {
                    let field = "yp1_show";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('显示|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "季卡显示", align: "left",
                field: "yp3_show",
                templet: function (d) {
                    let field = "yp3_show";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('显示|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "半年卡显示", align: "left",
                field: "yp6_show",
                templet: function (d) {
                    let field = "yp6_show";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('显示|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "年卡显示", align: "left",
                field: "np_show",
                templet: function (d) {
                    let field = "np_show";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('显示|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "允许节点退款", align: "left",
                field: "order_node_refund_switch",
                templet: function (d) {
                    let field = "order_node_refund_switch";
                    form.on("switch(" + field + ")", function (data) {
                        let load = layer.load();
                        let postData = {};
                        postData[field] = data.elem.checked ? 1 : 0;
                        postData[PRIMARY_KEY] = this.value;
                        $.post(UPDATE_API, postData, function (res) {
                            layer.close(load);
                            if (res.code) {
                                return layui.popup.failure(res.msg, function () {
                                    data.elem.checked = !data.elem.checked;
                                    form.render();
                                });
                            }
                            return layui.popup.success("操作成功");
                        })
                    });
                    let checked = d[field] === 1 ? "checked" : "";
                    return '<input type="checkbox" value="' + util.escape(d[PRIMARY_KEY]) + '" lay-filter="' + util.escape(field) + '" lay-skin="switch" lay-text="' + util.escape('开启|关闭') + '" ' + checked + '/>';
                },
                width: 120,
            },{
                title: "退款时间", align: "left",
                field: "order_node_refund_time",
                width: 100,
            }, {
                title: "退款数量", align: "left",
                field: "order_node_refund_number",
                width: 100,
            }, {
                title: "操作",
                toolbar: "#table-bar",
                align: "left",
                fixed: "right",
                width: 100,
            }
        ];

        // 渲染表格
        function render() {
            table.render({
                elem: "#data-table",
                url: SELECT_API,
                page: true, // 是否显示分页
                limits: [20,100, 200, 300,400,500,600,700,800,900,1000],
                limit: 100, // 每页默认显示的数量
                cols: [cols],
                skin: "line",
                size: "lg",
                toolbar: "#table-toolbar",
                autoSort: false,
                defaultToolbar: [{
                    title: "刷新",
                    layEvent: "refresh",
                    icon: "layui-icon-refresh",
                }, "filter", "print", "exports"],
                done: function () {
                    layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
                },
                where: { // 向服务端传递的额外参数
                    field: 'weight', // 默认排序的字段
                    order: 'desc' // 默认排序方式：asc（升序）或desc（降序）
                },
                initSort: {
                    field: 'weight', // 默认排序的字段
                    type: 'desc' // 默认排序方式：asc（升序）或desc（降序）
                },
            });
        }

        // 获取表格中下拉或树形组件数据
        let apis = [];
        apis.push(["line_id", "/app/admin/v1-line/select?format=tree&field=id&order=desc"]);
        let apiResults = {};
        apiResults["line_id"] = [];
        let count = apis.length;
        layui.each(apis, function (k, item) {
            let [field, url] = item;
            $.ajax({
                url: url,
                dateType: "json",
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }

                    function travel(items) {
                        for (let k in items) {
                            let item = items[k];
                            apiResults[field][item.value] = item.name;
                            if (item.children) {
                                travel(item.children);
                            }
                        }
                    }

                    travel(res.data);
                },
                complete: function () {
                    if (--count === 0) {
                        render();
                    }
                }
            });
        });
        if (!count) {
            render();
        }

        // 编辑或删除行事件
        table.on("tool(data-table)", function (obj) {
            if (obj.event === "remove") {
                remove(obj);
            } else if (obj.event === "edit") {
                edit(obj);
            }
        });

        // 表格顶部工具栏事件
        table.on("toolbar(data-table)", function (obj) {
            if (obj.event === "add") {
                add();
            } else if (obj.event === "refresh") {
                refreshTable();
            } else if (obj.event === "batchRemove") {
                batchRemove(obj);
            }
        });

        // 表格顶部搜索事件
        form.on("submit(table-query)", function(data) {
            data.field.field = PRIMARY_KEY;
            data.field.order = 'desc';
            console.log(data,'table-query');
            table.reload("data-table", {
                page: {
                    curr: 1
                },
                where: data.field
            })
            return false;
        });

        // 表格顶部搜索重置事件
        form.on("submit(table-reset)", function(data) {
            let data1 = {};
            data1.field = PRIMARY_KEY;
            data1.order = 'desc';
            console.log(data,'table-reset');
            table.reload("data-table", {
                where: data1
            })
        });

        // 字段允许为空
        form.verify({
            phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
            email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
            url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
            number: [/(^$)|^\d+$/,'只能填写数字'],
            date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
            identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
        });

        // 表格排序事件
        table.on("sort(data-table)", function(obj){
            let data = obj.config.where;
            data.field = obj.field;
            data.order = obj.type;
            console.log(data,'obj');
            table.reload("data-table", {
                initSort: obj,
                scrollPos: "fixed",
                where:data
                // where: {
                //     field: obj.field,
                //     order: obj.type
                // }
            });
        });

        // 表格新增数据
        let add = function() {
            layer.open({
                type: 2,
                title: "新增",
                shade: 0.1,
                maxmin: true,
                area: [common.isModile()?"100%":"60%", common.isModile()?"100%":"95%"],
                content: INSERT_URL,
                skin: ''
            });
        }

        // 表格编辑数据
        let edit = function(obj) {
            let value = obj.data[PRIMARY_KEY];
            layer.open({
                type: 2,
                title: "修改",
                shade: 0.1,
                maxmin: true,
                area: [common.isModile()?"100%":"60%", common.isModile()?"100%":"95%"],
                content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value,
                skin: ''
            });
        }

        // 删除一行
        let remove = function (obj) {
            return doRemove(obj.data[PRIMARY_KEY]);
        }

        // 删除多行
        let batchRemove = function (obj) {
            let checkIds = common.checkField(obj, PRIMARY_KEY);
            if (checkIds === "") {
                layui.popup.warning("未选中数据");
                return false;
            }
            doRemove(checkIds.split(","));
        }

        // 执行删除
        let doRemove = function (ids) {
            let data = {};
            data[PRIMARY_KEY] = ids;
            layer.confirm("确定删除?", {
                icon: 3,
                title: "提示"
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DELETE_API,
                    data: data,
                    dataType: "json",
                    type: "post",
                    success: function (res) {
                        layer.close(loading);
                        if (res.code) {
                            return layui.popup.failure(res.msg);
                        }
                        return layui.popup.success("操作成功", refreshTable);
                    }
                })
            });
        }

        // 刷新表格数据
        window.refreshTable = function () {
            table.reloadData("data-table", {
                scrollPos: "fixed",
                done: function (res, curr) {
                    if (curr > 1 && res.data && !res.data.length) {
                        curr = curr - 1;
                        table.reloadData("data-table", {
                            page: {
                                curr: curr
                            },
                        })
                    }
                }
            });
        }
    })

</script>
</body>
</html>
